package cn.shangqizhenshuai.sqiot.Serivce.impl;

import cn.shangqizhenshuai.sqiot.Serivce.UserService;
import cn.shangqizhenshuai.sqiot.entity.User;
import cn.shangqizhenshuai.sqiot.entity.UserExample;
import cn.shangqizhenshuai.sqiot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public boolean login(String username, String password) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(username).andPasswordEqualTo(password);
        List<User> users = userMapper.selectByExample(userExample);
        return users.size()>0?true:false;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean adduser(String username, String password) {
        User user = new User();
        user.setPassword(password);
        user.setUsername(username);
        int i = userMapper.insertSelective(user);
        return i>0?true:false;
    }
}
